package com.risk.modules.rm.selector;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class ServiceSelector<Service extends ServiceProvider> {

    private Map<Object, Service> map;

    protected void init(List<Service> serviceList) {
        map = new HashMap<>(serviceList.size());
        for (Service service : serviceList) {
            if (service.getType() != null) {
                map.put(service.getType(), service);
            }
        }
    }

    public Service select(Object type) {
        return map.get(type);
    }

    public Service register(Service service) {
        return map.putIfAbsent(service.getType(), service);
    }
}
